package com.liu.manager.service.impl;

import com.liu.manager.bean.ResponseResult;
import com.liu.manager.entity.Sysuser;
import com.liu.manager.mapper.*;
import com.liu.manager.service.SysuserService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;

@Service
public class SysuserSvcImpl implements SysuserService {

    @Resource
    SysuserMapper sysuserMapper;
    @Resource
    RolerAndPermissionMapper rolerAndPermissionMapper;
    @Resource
    UserAndRolerMapper userAndRolerMapper;
    @Resource
    SysrolerMapper sysrolerMapper;
    @Resource
    SyspermissionMapper syspermissionMapper;

    @Override
    public ResponseResult insertUser(Sysuser sysuser) {

        ResponseResult responseResult = new ResponseResult();

        if(!StringUtils.hasLength(sysuser.getName())){
            responseResult.setCode(-100);
            responseResult.setMsg("用户名为空");
            return responseResult;
        }

        Sysuser userByName = sysuserMapper.getUserByName(sysuser.getName());
        if(userByName != null){
            responseResult.setCode(-100);
            responseResult.setMsg("用户名重复");
            return responseResult;
        }

        if(!StringUtils.hasLength(sysuser.getPassword())){
            responseResult.setCode(-100);
            responseResult.setMsg("密码为空");
            return responseResult;
        }
        sysuser.setStatus(1);
        if(sysuser.getCreateTime() == 0) {
            sysuser.setCreateTime(System.currentTimeMillis());
            sysuser.setUpdateTime(System.currentTimeMillis());
        }
        long id = sysuserMapper.insertUser(sysuser);
        responseResult.setCode(100);
        responseResult.setMsg("创建成功");
        responseResult.setResult(sysuser);
        return responseResult;
    }

    @Override
    public ResponseResult deleteUser(long id) {
        ResponseResult responseResult = new ResponseResult();
        if(id <= 0){
            responseResult.setCode(-101);
            responseResult.setMsg("id错误");
            return responseResult;
        }
        responseResult.setCode(100);
        responseResult.setMsg("删除成功");

        return responseResult;
    }

    @Override
    public ResponseResult updateUser(Sysuser sysuser) {
        ResponseResult responseResult = new ResponseResult();
        if(sysuser.getId() <= 0){
            responseResult.setCode(-101);
            responseResult.setMsg("id错误");
            return responseResult;
        }
        Sysuser sysuser1 = sysuserMapper.getUserByid(sysuser.getId());
        if(StringUtils.hasLength(sysuser.getName())){
            sysuser1.setName(sysuser.getName());
        }

        if(StringUtils.hasLength(sysuser.getPassword())){
            sysuser1.setPassword(sysuser.getPassword());
        }

        sysuser1.setUpdateTime(System.currentTimeMillis());
        sysuser1.setStatus(1);

        sysuserMapper.updateUser(sysuser1);

        responseResult.setCode(100);
        responseResult.setMsg("更新成功");
        responseResult.setResult(sysuser1);

        return responseResult;
    }

    @Override
    public ResponseResult findById(long id) {
        ResponseResult responseResult = new ResponseResult();
        if(id <= 0){
            responseResult.setCode(-101);
            responseResult.setMsg("id错误");
            return responseResult;
        }
        responseResult.setCode(100);
        responseResult.setMsg("更新成功");
        responseResult.setResult(sysuserMapper.getUserByid(id));
        return responseResult;
    }

    @Override
    public ResponseResult login(Sysuser sysuser) {
        if(!StringUtils.hasLength(sysuser.getName())){
            return ResponseResult.ERROR("用户名为空");
        }
        if(!StringUtils.hasLength(sysuser.getPassword())){
            return ResponseResult.ERROR("密码为空");
        }
        Sysuser userByName = sysuserMapper.getUserByName(sysuser.getName());
        if(userByName == null || !userByName.equals(sysuser.getPassword())){
            return ResponseResult.ERROR("用户名或密码错误");
        }
        HashMap<String,Object> userinfo = new HashMap<>();
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(100);
        responseResult.setMsg("创建成功");
        responseResult.setResult(userByName);
        return null;
    }
}
